
// ============================获取产品id=================================
function getUrlParam(url, name) {
    var pattern = new RegExp("[?&]" + name + "\=([^&]+)", "g");
    var matcher = pattern.exec(url);
    var items = null;
    if (null != matcher) {
        try {
            items = decodeURIComponent(decodeURIComponent(matcher[1]));
        } catch (e) {
            try {
                items = decodeURIComponent(matcher[1]);
            } catch (e) {
                items = matcher[1];
            }
        }
    }
    return items;
}

var curProductId = getUrlParam(location.href, 'id');
var curProduct;


$.ajax({
    url: "http://116.62.9.171:8100/api/product/getproducts",
    contentType: "application/json; charset=utf-8",
    type: 'get',
    dataType: 'json',
    data: 2,
    success: function (data) {
        console.log('getProduct success');
        var item = data.data.data;
        for (var i = 0; i < item.length; i++) {
            if (item[i].product_id = curProductId) {
                curProduct = item[i];
            }
        }
        console.log(curProduct);
        loadEvent(curProduct);
    },
    error: function (err) {
        console.log('err', err);
    },
    complete: function () {
        console.log('complete');
    }

})

// ==============================解析产品id 选择参数=====================================


// 定义各种参数 currentModule是当前模板序号 totalModules是总模板数 currentPageAlgosAll是当期模板的排布算法集合 totalModuleAlgo指所有模板的算法数组

var leftBtn = document.getElementsByClassName('ifa_arrow_left')[0];
var rightBtn = document.getElementsByClassName('ifa_arrow_right')[0];

var curNumSpan = document.getElementById('ifa_zsj_curMod');
var totalNumSpan = document.getElementById('ifa_zsj_totalMod');
var board = document.getElementById('ifa_zsj_board');
var ctx = board.getContext('2d');
// 选取模板插入位置
var zBottomContent = document.getElementsByClassName('ifa_photo_list')[0];

// ==================================添加二次选取照片事件=============================


function loadEvent(item) {
    /*显示图片选择器*/
    var imagePicker = window.ifaImagePicker;
    imagePicker.minSelected = item.min_number;
    imagePicker.show();

    var imgPickerAfter, imgUploader;


    var Zeditor = new window.Zeditor;
    Zeditor._board = board;
    Zeditor._ctx = ctx;

    Zeditor._pageCount = item.page_number;
    
    // Zeditor._canvasInitHeight = item.product_height;
    // Zeditor._canvasInitWidth = item.product_width;

    // Zeditor._initRate = Zeditor._canvasInitWidth / Zeditor._canvasInitHeight;
    // Zeditor._borderScale=(Zeditor._initRate>1.5)?2:4;
    // Zeditor._toModuleRate = Zeditor._canvasInitHeight / Zeditor._moduleCanvasHeight;

    Zeditor._productId = item.product_id;

    Zeditor._pageCellGap = 30;
    Zeditor._pageBorder = 7;


    imagePicker.confirmBlock = function (selectedFiles) {
        $('#ifa_load_page').show();
        // //TODO:刷新布局和左边栏
        Zeditor.zFirstGetAllModule(selectedFiles);
        Zeditor.zAddListener();
    };

    $('.ifa_left_footer a').on('click', function () {
        imgPickerAfter = window.ifaImagePicker;
        imgPickerAfter.minSelected = 0;
        $('#ifa_zsj_fileInput').click();
    });

    $('#ifa_zsj_fileInput').on('change', function () {
        var self = this;
        // console.log(this.files);
        imgPickerAfter.show(function () {
            imgPickerAfter.addFiles(self.files);
            imgPickerAfter.confirmBlock = function (selectedFiles) {
                console.log(selectedFiles, 'selectedFiles');
                var zAfterFileInfo = Zeditor.infoToDic(selectedFiles);
                for (var k in zAfterFileInfo) {
                    Zeditor.appendToLeft(k, zAfterFileInfo[k], false);
                    Zeditor._keyNum += 1;
                }
                $('#ifa_zsj_photoNum').html(Zeditor._keyNum);
            }
        });
    });

    $('.ifa_photo_shopping').on('click', function () {

        var files = [];
        for (var i in Zeditor._zAllFileInfo) {
            var fileInfo = Zeditor._zAllFileInfo[i].fileInfo;
            files.push(fileInfo);
        }

        imgUploader = new IFAImageUploader();
        imgUploader.init(function () {
            imgUploader.addFiles(files);
            imgUploader.start();
            console.log(imgUploader);
        });

    });
}
